{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "squeeze_net_trainval_manual__os = pd.read_csv(\"squeeze_net_trainval_manual__os.csv\")\n",
    "squeeze_net_manual_scratch__os = pd.read_csv(\"squeeze_net_scratch_trainval_manual.csv\")\n",
    "squeeze_net_mirror_rot_trainval_manual__rots_os = pd.read_csv(\"squeeze_net_mirror_rot_trainval_manual_full.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ratios = (0.28, 0.23, 0.49)\n",
    "models = [squeeze_net_trainval_manual__os, squeeze_net_manual_scratch__os, squeeze_net_mirror_rot_trainval_manual__rots_os]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ensemble = models[0][['fname']].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for p in ['p0', 'p1', 'p2']:\n",
    "    ensemble[p] = np.sum([m[p] * r for m, r in zip(models, ratios)], axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ensemble.insert(1, 'class', ensemble[['p0', 'p1', 'p2']].as_matrix().argmax(axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>fname</th>\n",
       "      <th>class</th>\n",
       "      <th>p0</th>\n",
       "      <th>p1</th>\n",
       "      <th>p2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>66256bbe4743559c72037a30ef2c1007.jpg</td>\n",
       "      <td>1</td>\n",
       "      <td>1.298959e-04</td>\n",
       "      <td>9.993741e-01</td>\n",
       "      <td>0.000496</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>235c0036f76c52871c187b04fb517411.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>9.181579e-01</td>\n",
       "      <td>5.977498e-02</td>\n",
       "      <td>0.022067</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a489fa26f3097ad01eb3e7f011587288.jpg</td>\n",
       "      <td>1</td>\n",
       "      <td>3.642105e-08</td>\n",
       "      <td>9.999972e-01</td>\n",
       "      <td>0.000003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>aad400601b3b7dbecd746c9216d2fd08.jpg</td>\n",
       "      <td>2</td>\n",
       "      <td>5.314758e-09</td>\n",
       "      <td>3.873407e-07</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b015982c50a27b78b2d674419a5f12fc.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>8.442281e-01</td>\n",
       "      <td>2.669876e-02</td>\n",
       "      <td>0.129073</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  fname  class            p0            p1  \\\n",
       "0  66256bbe4743559c72037a30ef2c1007.jpg      1  1.298959e-04  9.993741e-01   \n",
       "1  235c0036f76c52871c187b04fb517411.jpg      0  9.181579e-01  5.977498e-02   \n",
       "2  a489fa26f3097ad01eb3e7f011587288.jpg      1  3.642105e-08  9.999972e-01   \n",
       "3  aad400601b3b7dbecd746c9216d2fd08.jpg      2  5.314758e-09  3.873407e-07   \n",
       "4  b015982c50a27b78b2d674419a5f12fc.jpg      0  8.442281e-01  2.669876e-02   \n",
       "\n",
       "         p2  \n",
       "0  0.000496  \n",
       "1  0.022067  \n",
       "2  0.000003  \n",
       "3  1.000000  \n",
       "4  0.129073  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ensemble.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ensemble.to_csv(\"nexar_submission3_ensemble.csv\", float_format='%.15f', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
